﻿using MD5Hash;
using Microsoft.EntityFrameworkCore;
using Month;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dal
{
    public class EroolDAL
    {
        public EroolDAL(MyContext context)
        {
            Context = context;
        }

        public MyContext Context { get; }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="e">实体</param>
        /// <returns>返回添加成功或失败</returns>
        public int EroolAdd(EroolTableInfor e)
        {
            try
            {
                Context.EroolTableInfor.Add(e);
                e.EPassword = e.EPassword.GetMD5();
                return Context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }



        }

        /// <summary>
        /// 登录功能
        /// </summary>
        /// <param name="LName">名称</param>
        /// <param name="LPassword">密码</param>
        /// <returns>返回成功或密码登录失败，账号登录失败</returns>
        public LoginTableInfor loginSel(string LName, string LPassword)
        {
            try
            {
                var ModelName = Context.LoginTableInfor.FirstOrDefault(c => c.LName == LName);
                if (ModelName == null)
                {

                    return new LoginTableInfor { LId = -1 };
                }
                if (ModelName.LPassword == LPassword.GetMD5())
                {

                    return ModelName;

                }
                return new LoginTableInfor { LId = -2 };
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
